package com.company.psm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.company.psm.entity.Medicine;
import com.company.psm.entity.User;
import com.company.psm.service.MedicineManagement;
import com.company.psm.service.UserManagement;

@Controller
@RequestMapping("/sales")
public class SaleController {
	@Autowired
	private MedicineManagement medicineManagement;
	@Autowired
	private UserManagement userManagement;
	@RequestMapping(method = RequestMethod.GET, value = "staffviewmedicine")
	public ModelAndView getAllStaffMedicine(@RequestParam(value="listAll", required=false) String listAll,
			@RequestParam(value="searchKey", required=false) String searchKey
			) {
		List<Medicine> medicines = new ArrayList<Medicine>();
		Long storeId = getCurrentUser().getStore().getId();
		System.out.println("StoreID = " + storeId);
		ModelAndView model = new ModelAndView();
		if (listAll!=null && listAll.equals("listAllData")){
			medicines = medicineManagement.getMedicineByStoreId(storeId);
		}else{
			if(searchKey!=null){
				medicines = medicineManagement.search(searchKey,storeId);
				model.addObject("searchKey",searchKey);
			}
		}
		model.setViewName("/sales/staffviewmedicine");
		model.addObject("medicines",medicines);
		return model;
	}
	@RequestMapping(method = RequestMethod.GET, value = "list")
	public ModelAndView getAllMedicine(@RequestParam(value="listAll", required=false) String listAll,
			@RequestParam(value="searchKey", required=false) String searchKey
			) {
		List<Medicine> medicines = new ArrayList<Medicine>();
		Long storeId = getCurrentUser().getStore().getId();
		System.out.println("StoreID = " + storeId);
		ModelAndView model = new ModelAndView();
		if (listAll!=null && listAll.equals("listAllData")){
			medicines = medicineManagement.getMedicineByStoreId(storeId);
		}else{
			if(searchKey!=null){
				medicines = medicineManagement.search(searchKey,storeId);
				model.addObject("searchKey",searchKey);
			}
		}
		model.setViewName("/medicine/list");
		model.addObject("medicines",medicines);
		return model;
	}
    public User getCurrentUser(){
    	Authentication authentication = SecurityContextHolder.getContext().
      		  getAuthentication();
      		  String name = authentication.getName();
      	return userManagement.getUserByAccount(name);
    }
	@RequestMapping(method=RequestMethod.GET,value = "search")
	public ModelAndView searchMedicine(@RequestParam(value="searchKey", required=false) String searchKey){
		ModelAndView model = new ModelAndView();
		model.setViewName("/sales/list");
		Long storeId = getCurrentUser().getStore().getId();
		List<Medicine> medicines = medicineManagement.search(searchKey,storeId);
		model.addObject("searchKey",searchKey);
		model.addObject("medicines",medicines);
		return model;
	}
}
